Instructions for creating a server application on nodejs + express

Attention

If you don’t have knowledge of JavaScript, you need to do something about it. For example, read the online tutorial: https://learn.javascript.ru/

Node.js what is it?

Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it light and efficient. Node.js’ package ecosystem, npm, is the largest ecosystem of open source libraries in the world.

Installation

Go to the official site https://nodejs.org/en/ to download it. On the main page we see two possible options for download: the latest version of NodeJS and the LTS version.

Download, install. There you can find instructions for setting up:

centos:

1
2
3
curl —silent —location https://rpm.nodesource.com/setup_9.x | sudo bash - 
sudo yum -y install nodejs
sudo yum install gcc-c++ make

ubuntu:

1
2
3
curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash - 
sudo apt-get install -y nodejs
sudo apt-get install -y build-essential

You can verify the installation by:

1
node --version

The command will show the current version of the program.

REPL

After installing NodeJS, we get a tool like REPL available to us. REPL (Read Event Printed Loop) represents the ability to run expressions in the JavaScript language on the command line or terminal.

So, run the command line (on Windows) or the terminal (on OS X or Linux) and enter the node command. After entering this command, we can execute various expressions in JavaScript.

Packages in Node.js

In short, a package in Node.js is one or more JavaScript files that represent a library or tool.

npm (node manager) is a standard package manager that automatically installs with Node.js. It is used to download packages from the npm cloud server, or to download packages to these servers.

Now, where to begin?

Create a project directory. Then open the terminal, and execute the command:

1
npm init

After answering the questions the file ‘package.json’ will be in the project directory

Express

Express is a minimalistic and flexible web framework for Node.js applications built on the connect framework.

Main purpose is routing and intermediate processing with minimal own functionality: Express application, in fact, is a series of operations of intermediate processing functions (middleware).

To set up the Express in the terminal from the project directory, execute the command:

1
npm install express

How to apply this

Create a file server.js.

To use the Express framework, you need to connect the express module, and create the application.

1
2
var express = require('express'); 
var app = express();

Run the application using the command:

1
2
3
app.listen(8080, function(){
console.log('server start on port 8080');
})

As a result, the server will start, but if you execute a query to application localhost: 8080, you get the error:
`Cannot GET /`
This is because there is no route in the program.

Routing

Routing determines how the application responds to a client request to a specific address, and to a specific HTTP request method.

Each route can have several handlers.

the following structure used to determine the route:

1
app.METHOD(path, handler)

app - the instance of the express application
METHOD - HTTP request method
path - the path to the server
handler - the function-handler

We continue to apply

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var express = require('express');
var app = express();

// We call the get method, it will set up a handler that will respond to get requests, for the route '/'
app.get('/', function(request, response){
console.log(request.url);
response.send('<h1>Hello, world!</h1>');
});

// Same for the '/ about' route
app.get('/about', function(request, response){
console.log(request.url);
response.send('<h1>About Page</h1>');
});
app.listen(8080);

What else?

Middleware-function

Middleware-functions are functions that have access to the request object req, the response object res, and to the next intermediate processing function in the “request-response” loop of the application next.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var express = require('express');
var app = express();

app.use('/', function(request, response, next){
console.log('Prehandler...');
// pass control to the next handler
next();
});

app.get('/', function(request, response){
console.log('Main handler');
// complete the response from the server
response.end();
});

app.listen(8080);

Thus, both functions are executed when accessing the root path.

Routing

Using the express.Router class, you can create modular, mounted route handlers. An instance of this class represents a complex system of intermediate handlers and routing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
var express = require('express');
var app = express();

var router = express.Router();

router.route("/")
.get(function(req, res){
res.send("List of products. Get method.");
})
.post(function(req, res){
res.send("Product created. POST method.");
});
router.route("/:id")
.get(function(req, res){

res.send(`Product ${req.params.id}`);
});

app.use("/products", router);

app.get("/", function(req, res){
res.send("Main page");
});

app.listen(8080);

When executing the localhost:8080/products query, the route will be determined from the router. The corresponding handler function will be executed.

Finish

In the manual, you learned about node.js+express. This will be enough for a quick start.
For more information refer to sources.

Sources:

node:
https://nodejs.org/en/
https://itvdn.com/ru/video/node_js/express - not free resource unfortunately, but useful.
https://learn.javascript.ru/screencast/nodejs - alternative

npm:
https://www.npmjs.com/

express:
http://expressjs.com/ru/
https://nodeguide.ru/doc/modules-you-should-know/express/
http://expressjs.com/ru/4x/api.html - express api directory